Method and apparatus for managing data transfer in a computer memory

ABSTRACT

The present invention relates to a method and apparatus for managing data transfer in a computer memory in which during the transfer of data from an application buffer to a network socket, memory usage faults are reduced.

FIELD OF INVENTION

The present invention relates to a method and apparatus for managing data transfer in a computer memory. More particularly, but not exclusively, the present invention relates to a method of transferring data from an application buffer to a network socket, in which copy avoidance is carried out adaptively so as to reduce memory usage faults.

BACKGROUND OF THE INVENTION

Typically, when data is transferred from one computer to another over a TCP/IP (Transfer Control Protocol/Internet Protocol) network, the application program data is first copied from application program memory space to a network buffer in kernel memory space. Application memory and kernel memory are separate memory spaces and are also referred to as protection domains as they protect the data in one domain from interference by programs in other domains. Once the data has been copied into kernel memory, the sockets and networking stack (TCP/IP stack) in the kernel are used to pass the data over the network. In a typical TCP/IP implementation many CPU (Central Processing Unit) cycles are consumed copying data across protection domains as outlined by David Clark et al in Analysis of TCP Processing Overhead, IEEE Communications Magazine, June 1989, Volume 27, Number 6.

One approach to reducing the burden on the CPU in copying data across domains is the use of zero copy techniques. Such techniques avoid the need for copying between protection domains, instead enabling data to be supplied directly to the network stack for transmission to its destination over the network. Zero copy implementations can provide significant increase in performance in terms of data throughput and CPU utilisation. There are two types of approaches to achieve zero copy implementation in a network stack as outlined by H K Jerry Chuin in Zero-Copy TCP in Solaris™, Proceedings of the USENIX, 1996 Annual Technical Conference, San Diego, Calif., January 1996. The first approach is based on shared buffer semantics and second approach is based on virtual copy semantics. Though shared buffer approaches may provide good solutions, they generally call for significant application program source code changes. Virtual copy approaches do not require such code changes. Generally on the send or transmit stage, zero copy is implemented using virtual memory (VM) support. One such method referred to as page remapping with Copy On Write (COW) is used by the Solaris™ operating system as outlined in Zero-Copy TCP in Solaris™ noted above. U.S. Pat. No. 5,182,805 discusses a COW mechanism.

In this zero copy approach, in order to avoid copying data from application program memory space into kernel (operating system) memory space, the application program memory address is remapped into the kernel memory space. In other words, the application memory space becomes a virtual part of the kernel memory space. However, if the application program attempts to change the contents of the remapped memory while it is being used by the kernel, a fault is generated which is referred to as a COW fault. The result of a COW fault is that the original application data is copied from the remapped memory space into kernel memory. As a result, a zero copy operation which generates a COW fault is more costly than the simple application to kernel copy. Some of applications are particularly susceptible to COW faults as outlined by Peter Druschel and Larry L. Peterson in Fbufs: A High-Bandwidth Cross-Domain Transfer Facility, Proceedings of the Fourteenth ACM Symposium on Operating Systems Principles, December 1993. Susceptibility to COW faults is dependent on various factors such as link speed, type of connection (TCP/UDP), round trip delay, and application behaviour.

It is an object of the present invention to provide a method or apparatus for managing data transfer in a computer memory, which reduces the overhead of dealing with COW faults. It is an object of the present invention to provide a method or apparatus for managing data transfer in a computer memory, which avoids some of the above disadvantages or at least provides the public with a useful choice.

SUMMARY OF THE INVENTION

According to a first aspect of the invention there is provided a method for managing data transfer in a computer memory comprising the steps of:

a) transferring data from an application program memory to a network stack using zero copy data transfer;

b) monitoring the number of potential copy on write (COW) faults generated during the transfer of data for the application program; and

c) controlling the transferring of data so that if the number of potential COW faults exceeds a predetermined threshold for the application then in subsequent data transfers from the application program, data is copied to another memory location before transferring the data to the network stack.

Preferably in step a) the zero copy data transfer is provided by page remapping. Preferably in step b) the monitoring is carried out for a plurality of application programs and the results of the monitoring are recorded in a record. Preferably the record entry for each application includes the port number, number of writes to the socket and the number of potential COW faults associated with the application. Preferably the other memory is kernel memory. Preferably the monitoring and controlling steps are carried out by the networking or socket code. Preferably if the number of potential COW faults for the application falls below the predetermined threshold then in subsequent data transfers reverting to the use of zero copy data transfer. Preferably the threshold is determined by the percentage of data transfers that produce potential COW faults for a given application. Preferably the threshold is 30%. Alternatively the threshold is 50%.

According to a second aspect of the invention there is provided apparatus for managing data transfer in a computer memory comprising:

a) transfer means operable to transfer data from an application program memory to a network stack using zero copy data transfer;

b) monitoring means operable to monitor the number of potential copy on write (COW) faults generated during the transfer of data for the application program; and

c) control means operable if the number of potential COW faults exceeds a predetermined threshold for the application to control subsequent data transfers from the application program so that data is copied to another memory location before transferring the data to the network stack.

According to a third aspect of the invention there is provided a computer program or group of computer programs arranged to enable a computer or group of computers to carry out a method for managing data transfer in a computer memory comprising the steps of:

a) transferring data from an application program memory to a network stack using zero copy data transfer;

b) monitoring the number of potential copy on write (COW) faults generated during the transfer of data for the application program; and

c) controlling the transferring of data so that if the number of potential COW faults exceeds a predetermined threshold for the application then in subsequent data transfers from the application program, data is copied to another memory location before transferring the data to the network stack.

According to a fourth aspect of the invention there is provided a computer program or group of computer programs arranged to enable a computer or group of computer provide apparatus for managing data transfer in a computer memory comprising:

a) transfer means operable to transfer data from an application program memory to a network stack using zero copy data transfer;

b) monitoring means operable to monitor the number of potential copy on write (COW) faults generated during the transfer of data for the application program; and

c) control means operable if the number of potential COW faults exceeds a predetermined threshold for the application to control subsequent data transfers from the application program so that data is copied to another memory location before transferring the data to the network stack.

According to a fifth aspect of the invention there is provided a method of controlling data transfer between an application program layer and a socket layer in a Unix operating system comprising:

a) transferring data using zero copy transfer;

b) monitoring the number of actual copy on write (COW) faults generated by an application; and

c) if the number of monitored COW faults exceeds a threshold for the application then transferring subsequent data using normal copy transfer.

Preferably, in step b) the potential COW faults are monitored and used in step c).

BRIEF DESCRIPTION OF THE DRAWINGS

Embodiments of the invention will now be described, by way of example only, with reference to the accompanying drawings in which: FIG. 1 is a schematic illustration of a computer network according to an embodiment of the present invention; and FIG. 2 is a flow chart illustrating the operation of a computer in the network of FIG. 1;

DETAILED DESCRIPTION OF A PREFERRED EMBODIMENT OF THE INVENTION

With reference to FIG. 1, a computer system 101 comprises a client computer 103 connected over an IP network 105 to a server computer 107 which in turn is connected to a storage device 109. The client computer 103 is running a version of the Unix™ operating system such as HP-UX™ which provides access over the network 105 to a database application program running on the server computer 107. The data used by the database application program is stored on the storage device 109 and is accessed over the network by the user of the client computer 103 via a client database application program.

The Unix™ operating system on the client computer 103 includes an application program module 111 and an operating system kernel module 113. The kernel module 113 is the central module of the operating system and is responsible for memory management, process and task management and disk management. The kernel 113 includes network sockets and a networking stack 115 which provides a software interface to the physical network 105. Each socket is identified by a designated number. The application program 111 can send and receive TCP/IP messages across the network 107 via the sockets and networking stack 115 by writing to one of the sockets provided. Some sockets are reserved for use by important or commonly used application programs.

When the application program 111 uses a designated socket to communicate with the database application running on the server computer 107, the application program performs a write on the socket and the contents of an application program buffer holding the data to be transmitted is copied to a kernel module buffer. The kernel module then uses its sockets and networking stack to transmit the data over the network 105. When page remapping is used to provide a zero copy data transmission, the address of the application buffer is remapped into the kernel module's address space thus becoming virtual memory for the kernel module 113. This saves the overhead of copying the data between application and kernel address spaces. The kernel simply accesses the remapped buffer and passes the data to the network socket module 115. If the application program 111 attempts to change the contents of the remapped memory while it is being used by the kernel module 113, a fault is generated which is referred to as a COW fault. The result of a COW fault is that the original application data is copied from the remapped memory space into kernel memory. As a result the zero copy operation generates a COW fault and is more costly than an application to kernel copy (a normal copy).

In the present embodiment, the operating system of the client computer 103 implements zero copy data transfers adaptively. In other words, the zero copy mechanism is selectively activated at run time based on recorded behaviour of application programs. If a given application program appears to be prone to COW faults then the zero copy mechanism is turned off. This avoids the processing penalty associated with COW faults. The operating system compiles a history record of writes and potential COW faults associated with socket and application program pairs. The recorded behaviour of the application is the potential COW faults as opposed to actual COW faults using a mechanism which will be described further below. This mechanism ensures that even if zero copy transfer is deactivated for a given application program, the potential COW faults for the application are still monitored and recorded.

Once the number of writes for any given socket and application pair reaches a threshold, the recorded ratio of potential COW faults to writes is used to determine whether the zero copy mechanism should be active or inactive for subsequent writes by the applications on the paired socket. For example, if more than 25% of writes by a given application on a given socket potentially produce COW faults then the zero copy mechanism may be deactivated for subsequent writes for the pair. The threshold for number of writes to a given socket pair that enables the history record to collect sufficient data is determined by testing. Similarly, the threshold percentage of potential COW faults for deactivating the zero copy mechanism is determined by testing. Both thresholds can be tuned to provide optimum performance in an active operating system.

Actual COW faults are detected automatically by the memory management system in the kernel. However, if the zero copy mechanism is deactivated then no COW faults will occur. In the present embodiment, the history record is used to record the location of the application buffer that is used for any given write that is in progress. This process is carried out regardless of whether or not the zero copy mechanism is activated. If a subsequent write uses any part of the buffer of a previous write that is not yet complete then a potential COW fault is generated. Once any given write is complete its record in the history record is removed.

The processing of the operating system will now be described with reference to the flow chart of FIG. 2 which is triggered at step 201 when an application program initiates a network (TCP or UDP) write. Processing moves to step 203 where a check is carried out to see if the zero copy mechanism is active or not. If not processing moves to step 205 where a normal copy of the data from the application program buffer into the kernel buffer is performed and processing moves to step 207 where the kernel module 115 passes the data to the network socket module 207.

If at step 203 the zero copy mechanism is active then processing moves to step 209 where the history record is queried to check if the number of writes for the application program and socket pair has exceeded the learning threshold for the history record. If not, processing moves to step 211 where a learning state variable is set to “Learn” and processing continues to step 213 where the COW fault monitoring process is activated for the duration of the current socket write by logging the buffer being used in the history record. Processing then moves to step 215 where if the history record is in learning mode (determined by the state variable noted above) then processing moves to step 217 where the zero copy mechanism is avoided and a normal copy process for the data transfer is carried out as described above for step 205. Processing then moves to step 219 where the COW fault monitoring process checks for potential faults for the current write by searching for any existing write in the history record which uses the same memory space as the current write. If such an existing write is found a potential COW fault is logged against the entry for the current application/socket pair in the history record. Also, the history record entry for the application/socket pair is updated to increment the number of writes for the socket/application pair and to remove any entries for completed writes. Processing then moves to step 207 where the network socket layer carries out the requested write as described above.

If at step 209 the learning threshold has been exceeded then processing moves to step 223 where the history record entry for the current application/socket pair is processed to establish if the COW fault threshold has been exceeded. In the present embodiment, the COW fault threshold is 30%, that is, 30% of writes to the pair have produced potential faults. If the number of potential COW faults so far detected is below the threshold then a state variable is set to “Active”. If however the threshold has been exceeded then the state variable is set to “Non-active”. In some cases, the ratio of potential COW faults to writes detected for a given application/socket pair may reduce thereby reintroducing the zero copy mechanism for that pair which had previously been deactivated.

Processing then moves to step 213 starting the fault monitor as described above and on through step 215 to step 221. At step 221, if the state variable noted above is set to “Non-active” then processing moves to step 217 where the normal copy mechanism is used to transfer the data. If however, at step 221 the zero copy state variable is set to “Active” then processing moves to step 225 where the zero copy mechanism is used to perform the transfer of data from the application module to the network socket module. After both steps 217 and 225, processing moves to step 219 where the history record entry for the application/socket pair is updated to increment the number of writes and the number of potential COW faults if any have been detected. Processing then moves to step 207 where the network socket layer carries out the requested write as described above.

An example of the data gathered by the history record in the learn mode described above is set out below: Socket Id. Application Port Number Writes Faults 0x49623700 SOCK_DGRAM 49372 1 0 0x49623700 SOCK_STREAM 1712 1 0 0x49623400 SOCK_STREAM 49355 1 0 0x49623400 SOCK_STREAM 49198 2 0 0x494ebcc0 SOCK_STREAM 49357 2 0 0x49623700 SOCK_STREAM 49198 2 0 0x48fe2980 SOCK_STREAM 49358 2 0 0x4953de00 SOCK_STREAM 49198 2 0 0x494eb6c0 SOCK_STREAM 49359 2 0 0x4953de00 SOCK_DGRAM 49373 1 0 0x4953de00 SOCK_DGRAM 49374 1 0 0x4953de00 SOCK_DGRAM 49375 1 0 0x4953de00 SOCK_DGRAM 49376 1 0 0x48cf6e40 SOCK_STREAM 1933 97 33

In the scenario above most of the data gram traffic does not generate potential COW faults thereby zero copy can be activated for those connections. However, after nearly 100 writes, 33 of the writes of the SOCK_STREAM program potentially produce COW faults. This data is used to define the learning threshold for this application/socket pair or connection. The zero copy threshold can set by comparing system performance with the threshold set at different levels.

In an alternative embodiment, instead of potential COW faults being monitored as noted above, the actual COW faults generated by the memory management system are monitored instead. While no such faults will occur while the zero copy mechanism is deactivated, useful data can be gathered while the mechanism is active. This data can then be used to adaptively activate or deactivate the mechanism as described above.

It will be understood by those skilled in the art that the apparatus that embodies a part or all of the present invention may be a general purpose device having software arranged to provide a part or all of an embodiment of the invention. The device could be single device or a group of devices and the software could be a single program or a set of programs. Furthermore, any or all of the software used to implement the invention can be communicated via various transmission or storage means such as computer network, floppy disc, CD-ROM or magnetic tape so that the software can be loaded onto one or more devices.

While the present invention has been illustrated by the description of the embodiments thereof, and while the embodiments have been described in considerable detail, it is not the intention of the applicant to restrict or in any way limit the scope of the appended claims to such detail. Additional advantages and modifications will readily appear to those skilled in the art. Therefore, the invention in its broader aspects is not limited to the specific details representative apparatus and method, and illustrative examples shown and described. Accordingly, departures may be made from such details without departure from the spirit or scope of applicant's general inventive concept. 

1. A method for managing data transfer in a computer memory comprising the steps of: a) transferring data from an application program memory to a network stack using zero copy data transfer; b) monitoring the number of potential copy on write (COW) faults generated during said transfer of data for said application program; and c) controlling the transferring of data so that if the number of potential COW faults exceeds a predetermined threshold for said application then in subsequent data transfers from said application program, data is copied to another memory location before transferring said data to said network stack.
 2. A method according to claim 1 in which in step a) the zero copy data transfer is provided by page remapping.
 3. A method according to claim 1 in which in step b) said monitoring is carried out for a plurality of application programs and the results of said monitoring are recorded in a record.
 4. A method according to claim 3 in which the record entry for each application includes the port number, number of writes to the socket and the number of potential COW faults associated with said application.
 5. A method according to claim 1 in which said other memory is kernel memory.
 6. A method according to claim 1 in which said monitoring and controlling steps are carried out by the networking or socket code.
 7. A method according to claim 1 further comprising the step of: d) if the number of potential COW faults for said application falls below said predetermined threshold then in subsequent data transfers reverting to the use of zero copy data transfer.
 8. A method according to claim 1 in which the threshold is determined by the percentage of data transfers that produce potential COW faults for a given application.
 9. A method according to claim 8 in which the threshold is 30%.
 10. A method according to claim 7 in which the threshold is 50%.
 11. Apparatus for managing data transfer in a computer memory comprising: a) transfer means operable to transfer data from an application program memory to a network stack using zero copy data transfer; b) monitoring means operable to monitor the number of potential copy on write (COW) faults generated during said transfer of data for said application program; and c) control means operable if the number of potential COW faults exceeds a predetermined threshold for said application to control subsequent data transfers from said application program so that data is copied to another memory location before transferring said data to said network stack.
 12. Apparatus according to claim 11 in which the zero copy data transfer is provided by page remapping.
 13. Apparatus according to claim 1 in which said monitoring means is operable to monitor a plurality of application programs and the results of said monitoring are recorded in a record
 14. Apparatus according to claim 13 in which the record entry for each application includes the port number, number of writes to the socket and the number of potential COW faults associated with said application.
 15. Apparatus according to claim 11 in which said other memory is kernel memory.
 16. Apparatus according to claim 11 in which said monitoring and controlling means are provided by the networking or socket code.
 17. Apparatus according to claim 11 in which the control means is further operable, if the number of potential COW faults for said application falls below said predetermined threshold, to revert to the use of zero copy data transfer in subsequent data transfers.
 18. Apparatus according to claim 11 in which the threshold is determined by the percentage of data transfers that produce potential COW faults for a given application.
 19. Apparatus according to claim 18 in which the threshold is 30%.
 20. Apparatus according to claim 17 in which the threshold is 50%.
 21. A computer program or group of computer programs arranged to enable a computer or group of computers to carry out the method of claim
 1. 22. A computer program or group of computer programs arranged to enable a computer or group of computer provide the apparatus of claim
 11. 23. A method of controlling data transfer between an application program layer and a socket layer in a Unix operating system comprising: a) transferring data using zero copy transfer; b) monitoring the number of actual copy on write (COW) faults generated by an application; and c) if the monitored number of COW faults exceeds a threshold for said application then transferring subsequent data using normal copy transfer.
 24. A method according to claim 23 in which in step b) the potential COW faults are monitored and used in step c). 